//
//  TPFeedbackViewController.m
//  Tripvi
//
//  Created by xiaowuxiaowu on 2017/6/21.
//  Copyright © 2017年 xiaowuxiaowu. All rights reserved.
//

#import "TPFeedbackViewController.h"
#import "UITextView+PlaceHolder.h"
@interface TPFeedbackViewController ()
@property (nonatomic,weak)UITextView *textView;
@end

@implementation TPFeedbackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"意见反馈";
    
    UITextView *textView = [[UITextView alloc]init];
    textView.backgroundColor = TP_COLOR_240;
    ViewRadius(textView, 2.0f);
    textView.font = fontSystemOfSize(16.0f);
    [self.view addSubview:textView];
    
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-10.0f);
        make.left.mas_equalTo(10.0f);
        make.top.mas_equalTo(IOS7_TOP_Y+20.0f);
        make.height.mas_equalTo(150.0f);
    }];
//    [textView addPlaceHolder:AppLocalString(@"Add Describe")];
    @weakify(textView);
    _textView = textView;
    
    UILabel *numberLabel = [UILabel initWithFrame:CGRectZero font:fontSystemOfSize(11.0) color:[UIColor lightGrayColor] aligment:NSTextAlignmentRight];
    numberLabel.text = [NSString stringWithFormat:@"不超过%ld字",(long)200];
    [self.view addSubview:numberLabel];
    [numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(textView.mas_right).offset(-10.0f);
        make.bottom.mas_equalTo(textView.mas_bottom);
        make.height.mas_equalTo(25.0f);
        make.width.mas_equalTo(140.0f);
    }];
    
    
    [textView.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        @strongify(textView);
        
        if([x isEqualToString:@"\n"]){
            [textView resignFirstResponder];
        }
        if(x.length > 200){
            textView.text = [x substringToIndex:200];
            
        }
        numberLabel.text = [NSString stringWithFormat:@"不超过%ld字",200-textView.text.length];
        
    }];
    
    
   
    
    
    @weakify(self);
    TPButton *submitButton = [TPButton buttonWithTitle:@"提交" toHeight:44.0 ActionHandler:^(NSInteger tag, UIButton *button) {
        @strongify(self);
        [self.view endEditing:YES];
        [self submitButton:button];
    }];
    [submitButton tp_arrow];
    [self.view addSubview:submitButton];
    [submitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.8);
        make.top.mas_equalTo(textView.mas_bottom).offset(50.0f);
        make.height.mas_equalTo(44.0f);
        make.centerX.mas_equalTo(self.view);
    }];
    
    
}


-(void)submitButton:(UIButton *)sender{
  
    [sender tp_startAnimating];
       [[TPRequestClient sharedClient]getFeedbackMessage:_textView.text success:^(NSURLSessionDataTask *task, id responseObject, Responese *respone) {
           
           [sender tp_stopAnimating];
           [UIView xw_hideTitle:respone.message];
           
           dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
               [self leftBtnAction:nil];
           });
           
       } failure:^(NSURLSessionDataTask *task, NSError *error, Responese *respone) {
           
            [sender tp_stopAnimating];
            [UIView xw_hideTitle:respone.message];
           
       } nonetWork:^(NSURLSessionDataTask *task, NSError *error) {
           [sender tp_stopAnimating];
       }];

}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.textView becomeFirstResponder];
}

-(void)dealloc{

    self.textView = nil;
}

@end
